package com.code.google.mdsc.gui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.code.google.mdsc.control.Business;
import com.code.google.mdsc.gui.i18n.Messages;
import com.code.google.mdsc.model.Creditor;

public class CreditorWindow {

	private static final int INSET = 10;
	private final JDialog dialog;

	public CreditorWindow(final Business business, final MainWindow mw) {

		dialog = new JDialog(mw.getMainFrame(),
				Messages.getString("CREDITOR_EDIT"));

		Container c = dialog.getContentPane();

		c.setLayout(new GridBagLayout());

		final JLabel creditorIdentifierLabel = new JLabel(
				Messages.getString("CREDITOR_IDENTIFIER"));
		final JLabel countryLabel = new JLabel(
				Messages.getString("CREDITOR_COUNTRY"));
		final JLabel ibanLabel = new JLabel(Messages.getString("CREDITOR_IBAN"));
		final JLabel bicLabel = new JLabel(Messages.getString("CREDITOR_BIC"));
		final JLabel creditorNameLabel = new JLabel(
				Messages.getString("CREDITOR_NAME"));
		final JLabel creditorAdressLabel = new JLabel(
				Messages.getString("CREDITOR_ADRESS"));

		final JTextField creditorIdentifier = new JTextField(business
				.getCreditorData().getIdentifier());
		final JTextField country = new JTextField(business.getCreditorData()
				.getCountry());
		final JTextField iban = new JTextField(business.getCreditorData()
				.getIBAN());
		final JTextField bic = new JTextField(business.getCreditorData()
				.getBIC());
		final JTextField creditorName = new JTextField(business
				.getCreditorData().getName());
		final JTextField creditorAdress = new JTextField();

		for (String s : business.getCreditorData().getAddress()) {
			creditorAdress.setText(s + "\n");
		}

		JButton save = new JButton(Messages.getString("CREDITOR_SAVE"));
		JButton cancel = new JButton(Messages.getString("CREDITOR_CANCEL"));
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});

		save.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Creditor c = business.getCreditorData();
				c.setBIC(bic.getText());
				c.setIBAN(iban.getText());
				c.setCountry(country.getText());
				c.setIdentifier(creditorIdentifier.getText());
				c.setName(creditorName.getText());

				c.addAdressLine(creditorAdress.getText());
				dialog.dispose();
			}
		});

		GridBagConstraints gbc = new GridBagConstraints();

		Insets in = new Insets(INSET, INSET, INSET, INSET);

		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.insets = in;
		gbc.gridx = 0;
		gbc.gridy = 0;
		c.add(creditorNameLabel, gbc);

		gbc.gridx = 1;
		c.add(creditorName, gbc);
		gbc.gridy++;

		gbc.gridx = 0;
		c.add(creditorIdentifierLabel, gbc);

		gbc.gridx = 1;
		c.add(creditorIdentifier, gbc);
		gbc.gridy++;
		gbc.gridx = 0;
		c.add(bicLabel, gbc);

		gbc.gridx = 1;
		c.add(bic, gbc);
		gbc.gridy++;
		gbc.gridx = 0;
		c.add(ibanLabel, gbc);

		gbc.gridx = 1;
		c.add(iban, gbc);
		gbc.gridy++;
		gbc.gridx = 0;
		c.add(countryLabel, gbc);

		gbc.gridx = 1;
		c.add(country, gbc);
		gbc.gridy++;
		gbc.gridx = 0;
		c.add(creditorAdressLabel, gbc);

		gbc.gridx = 1;
		c.add(creditorAdress, gbc);
		gbc.gridy++;
		gbc.gridx = 0;
		c.add(save, gbc);

		gbc.gridx = 1;
		c.add(cancel, gbc);

		dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		dialog.pack();
		dialog.setResizable(false);
		dialog.setVisible(true);
	}
}
